﻿using System;
using System.IO;
using System.Text;
using System.Reflection;
using System.Diagnostics;

using log4net;

namespace TerryDong.Core
{
    [assembly:log4net.Config.XmlConfigurator(Watch = true)]
    public class LogHelper
    {
        /// <summary>
        /// 日志客户端使用类型
        /// </summary>
        enum LogClientType
        {
            Customized,
            Log4Net
        }
        static LogClientType logClientType;
        static LogHelper()
        {
            logClientType = LogClientType.Log4Net;
        }

        public static void WriteError(string errrorMessage)
        {
            if (logClientType==LogClientType.Customized)
            {
                LogWriter.WriteErrLog(errrorMessage);
            }

            if (logClientType== LogClientType.Log4Net)
            {
                ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                log.Error(errrorMessage);
            }
        }

        public static void WriteWarning(string warningMesage)
        {
            if (logClientType == LogClientType.Customized)
            {
                LogWriter.WriteAccessLog(warningMesage);
            }
            if (logClientType == LogClientType.Log4Net)
            {
                ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                log.Warn(warningMesage);
            }
        }

        public static void WriteAccess(string accessMessage)
        {
            if (logClientType == LogClientType.Customized)
            {
                LogWriter.WriteAccessLog(accessMessage);
            }
            if (logClientType == LogClientType.Log4Net)
            {
                ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                log.Info(accessMessage);
            }
        }
    }

    internal static class LogWriter
    {
        private static string m_LogDirPath = null;
        private const string LOG_FORMAT = "{0:T}\t{1}:{2}";

        static LogWriter()
        {
            m_LogDirPath=Path.Combine(AppContext.AppPath,@"log\");
            if (!Directory.Exists(m_LogDirPath))
                Directory.CreateDirectory(m_LogDirPath);
        }

        private static string getLogFilePath(string type)
        {
            StringBuilder sb = new StringBuilder(m_LogDirPath);
            sb.Append(DateTime.Now.ToString("yyyyMMdd"));
            sb.Append("_");
            sb.Append(type);
            sb.Append(".txt");
            return sb.ToString();
        }


		public static void WriteAccessLog(string causer, string msg)
		{
            StreamWriter sw = new StreamWriter(getLogFilePath("access"), true, Encoding.Default);
            sw.WriteLine(LOG_FORMAT, DateTime.Now, causer, msg);
			sw.Close();  
		}

		public static void WriteAccessLog(string msg)
		{
            WriteAccessLog(string.Empty, msg);  
		}

        public static void WriteErrLog(string causer, string msg)
        {
            StreamWriter sw = new StreamWriter(getLogFilePath("error"), true, Encoding.Default);
            sw.WriteLine(LOG_FORMAT, DateTime.Now, causer, msg);
            sw.Close();  
        }

        public static void WriteErrLog(string msg)
        {
            StackFrame sf = new StackFrame(3);
            MethodBase mb = sf.GetMethod();
            WriteErrLog(String.Format("{0}.{1}", mb.ReflectedType, mb.Name), msg);
        }

        public static void WriteWarningLog(string causer, string msg)
        {
            StreamWriter sw = new StreamWriter(getLogFilePath("warning"), true, Encoding.Default);
            sw.WriteLine(LOG_FORMAT, DateTime.Now, causer, msg);
            sw.Close();
        }

        public static void WriteWarningLog(string msg)
        {
            StackFrame sf = new StackFrame(3);
            MethodBase mb = sf.GetMethod();
            WriteWarningLog(String.Format("{0}.{1}", mb.ReflectedType, mb.Name), msg);
        }
    }
}

